<?php
    session_start();
    
    $app_id = "653497694696178";
    $app_secret = "196db13df6893f00af222b14ed4ca722";
    $redirect_uri = urlencode("http://localhost/bluenow/callback.php");    
    
	$config['appId']='653497694696178'; // là giá trị của app ID khi ta cài đặt ứng sụng
	$config['secret']='196db13df6893f00af222b14ed4ca722'; // là giá tri App Secret
	$config['fileUpload']=true; // Để là true vì ta sẻ upload hình ảnh lên facebook
	
	$facebook = new Facebook($config); // khởi tạo
	
	$userid = $facebook->getUser(); //Lấy ID của người dùng
	
	$user = $facebook->api('/me'); //Lấy thông tin người dùng

	print_r($user);
	
      // Get code value
    $code = $_GET['code'];
    
    // Get access token info
    $facebook_access_token_uri = "https://graph.facebook.com/oauth/access_token?client_id=$app_id&redirect_uri=$redirect_uri&client_secret=$app_secret&code=$code";    
    
    $ch = curl_init(); 
    curl_setopt($ch, CURLOPT_URL, $facebook_access_token_uri);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);    
        
    $response = curl_exec($ch); 
    curl_close($ch);
    
    // Get access token
    $aResponse = explode("&", $response);

    $access_token = str_replace('access_token=', '', $aResponse[0]);
    
    // Get user infomation
    $ch = curl_init(); 
    curl_setopt($ch, CURLOPT_URL, "https://graph.facebook.com/me?access_token=$access_token");
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);    
        
    $response = curl_exec($ch); 
    curl_close($ch);
    
    $user = json_decode($response);

    //print_r($user);
    // Log user in
    $_SESSION['user_login'] = true;
    $_SESSION['user_name'] = $user->username;
    
    echo "Wellcome ". $user->username ."!";  
	 //require("index.php");

?>